<template>
	<view style="display: ;">
		<view class="search-box">
			<!-- mSearch组件 如果使用原样式，删除组件元素-->
			<!-- <mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword"
				@search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch> -->
			<!-- 原样式 如果使用原样式，恢复下方注销代码 -->

			<!-- <view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
			</view>
			<view class="search-btn" @tap="doSearch(false)">搜索</view> -->
			<u-search style="width: 100%;" placeholder="搜大神" :focus="true" v-model="keyword" :show-action="true"
				:animation="true" shape="square" bg-color="#343546" color="#FFFFFF" action-text="取消" @custom="goBack()"
				@search="doSearch(false)"></u-search>
			<!-- 原样式 end -->
		</view>
		<view class="search-keyword">
			<scroll-view v-show="isShowKeywordList" scroll-y>
				<view class="topBarBox">
					<view :class="[currentListIndex==1?'topBar_active':'topBar']" @click="changeTopBar(1)">
						<text space='emsp'>陪 玩</text>
					</view>
					<view :class="[currentListIndex==2?'topBar_active':'topBar']" @click="changeTopBar(2)"><text
							space='emsp'>用 户</text>
					</view>
				</view>

				<!-- currentListIndex == 1 循环商品 -->
				<view v-if="currentListIndex==1" class="">
					<view style="color: white;text-align: center;margin-bottom: 10rpx;">
						<text space="ensp">
							找到 &nbsp;
							<text style="color: #3390FF;">{{keyword}} </text>
							相关陪玩
							<text style="color: #3390FF;"> {{goodsList.length}} </text>
							个
						</text>
					</view>

					<view class="flex justify-between bg margin-lr-sm padding-sm radius"
						style="display: flex; flex-wrap: wrap; justify-content: space-between">
						<view v-for="(goods,index) in keywordList">
							<view @click="getDetails(goods.id)">
								<view class="u-relative">
									<u-image width="200upx" height="200upx" :src="goods.user.avr"></u-image>
									<view class="u-abso"
										style="bottom: 0;left: 0;color: white;background: rgba(0,0,0,.4);padding: 0 5upx;">
										<view
											style="width: 10upx;height: 10upx;background: #1AD566;border-radius: 10upx;display: inline-block;margin: 0 8upx 4upx 0;">
										</view>
										在线中
									</view>
								</view>
								<view class="margin-top-xs text-white text-df">
									{{goods.goodsCategory.name}}
								</view>
								<view class="margin-top-xs" style="color: #FF711B;">
									<u-image width="20upx" height="20upx"
										style="display: inline-block;margin-right: 6upx;"
										src="@/static/images/index/jinbi.png"></u-image>
									{{goods.money}}币/局
								</view>
							</view>
						</view>
					</view>
				</view>

				<!-- currentListIndex == 2 循环用户 -->
				<view v-if="currentListIndex==2" class="margin-lr-sm">
					<view style="color: white;text-align: center;margin-bottom: 10rpx;">
						<text space="ensp">
							找到
							<text style="color: #3390FF;"> {{keyword}} </text>
							相关用户
							<text style="color: #3390FF;"> {{userList.length}} </text>
							个
						</text>
					</view>
					<view v-for="(user,index) in userList" @click="getDetails(user.id)">
						<view class="flex justify-between margin-bottom bg padding-sm" style="display: flex;">
							<u-image :src="user.avr" width="200upx" height="200upx" mode="">
							</u-image>
							<view class="flex-sub margin-left text-white flex flex-direction justify-between"
								style="display: flex;">
								<view class="flex justify-between">
									<view>
										<view class="margin-right-xs" style="display: inline-block;">
											<text space='emsp'>{{user.nick}}</text>
										</view>
										<view class="round" v-if="user.gender=='女'"
											style="display: inline-block;background-color: #582D4D; color: #FF659A;padding: 2upx 8upx;">
											<u-icon name="woman" color="#FF659A" size="24"></u-icon>{{user.age}}
										</view>
										<view class="round" v-if="user.gender=='男'"
											style="display: inline-block;background-color: #4b56f1; color: #69a5ff;padding: 2upx 8upx;">
											<u-icon name="man" color="#69a5ff" size="24"></u-icon>24
										</view>
									</view>
								</view>
								<view class="flex radius"
									style="background: url(@/static/images/index/bg.png)100% no-repeat;line-height: 34upx;">

									<text class="margin-left-xs margin-right-sm">{{user.signature}}</text>
								</view>
								<view class="flex" style="color: #3390FF;">
									<view class="margin-right">评分5.0</view>
									<text space="emsp">服务 {{user.totalSaleNums}} 人 粉丝：{{user.fansCount}}</text>
								</view>
								<view><text style="color: #FF6F1B;">7币 </text> /局</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
			<view class="keyword-block">
				<view class="keyword-list-header">
					<view>热门搜索</view>
					<view>
						<image @tap="hotToggle" :src="'@/static/images/index/attention'+forbid+'.png'"></image>
					</view>
				</view>
				<view class="keyword" v-if="forbid==''">
					<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
						{{keyword}}
					</view>
				</view>
				<view class="hide-hot-tis" v-else>
					<view>当前热搜已隐藏</view>
				</view>
			</view>
			<view class="keyword-block" v-if="oldKeywordList.length>0">
				<view class="keyword-list-header">
					<view>历史记录</view>
					<view>
						<image @tap="oldDelete" src="/static/images/index/delete.png"></image>
					</view>
				</view>
				<view class="keyword">
					<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
						{{keyword}}
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [], //历史记录
				hotKeywordList: ["英雄联盟", "王者荣耀", "绝地求生", "最强王者"], //热搜
				keywordList: [], //搜索列表
				forbid: '',
				isShowKeywordList: false,
				limit: 10,
				page: 1,
				currentListIndex: 1,
				userList: [],
				goodsList: [],
				fansCount: 0,
				likesCount: 0
			}
		},
		onLoad() {
			// this.init();
			this.getSearchList();

			this.$myRequest({
				url: '/user/byId', // 请求路径
				method: "get", // 请求方法
				data: {
					id: 1
				}
			}).then(r => {
				console.log(r);
			});
		},
		methods: {
			// 获取搜索历史
			getSearchList() {
				this.oldKeywordList = JSON.parse(uni.getStorageSync('searchList') || '[]')
				console.log('onshow', this.oldKeywordList)
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.$u.api.SearchDet().then(res => {
								if (res.code == 0) {
									this.getSearchList()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//执行搜索
			doSearch(keyword) {
				// 搜索的事件

				// 缓存搜索历史
				if (keyword == '' && this.keyword == '') {
					uni.showToast({
						title: '搜索内容不能为空',
						icon: "none"
					})
					return;
				} else {
					if (this.oldKeywordList.includes(keyword)) {
						let i = this.oldKeywordList.indexOf(keyword);
						this.oldKeywordList.splice(i, 1);
						this.oldKeywordList.unshift(keyword)
					} else {
						this.oldKeywordList.unshift(keyword)
						uni.setStorageSync('searchList', JSON.stringify(this.oldKeywordList))
					}
				}

				this.keyword = keyword === false ? this.keyword : keyword;
				this.isShowKeywordList = true;

				this.$myRequest({
					url: "/search/" + this.keyword
				}).then(r => {
					if (r.data.code == 200) {
						console.log("搜索结果", r);
						this.userList = r.data.data.userList;
						this.goodsList = r.data.data.goodsList;
						this.keywordList = this.goodsList;
					}
				})
			},
			// 点击取消返回首页
			goBack() {
				uni.navigateBack()
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			// 更改底部标签 商品 用户
			changeTopBar(barId) {
				this.currentListIndex = barId;
				if (barId == 1) {
					this.keywordList = this.goodsList;
				} else {
					this.keywordList = this.userList;
				}
			},
			// 点击查看详情
			getDetails(id) {
				if (this.currentListIndex == 1) {
					// 跳转至商品详情页
					console.log("商品id：" + id);
					uni.navigateTo({
						url: '/pages/index/game/order?id=' + id
					})
				} else {
					// 跳转至用户主页
					console.log("用户id：" + id)
					uni.navigateTo({
						url: '/pages/index/game/user?id=' + id
					})
				}
			}

		},
		onReachBottom: function() {
			this.page = this.page + 1;
			this.doSearch(false);
		},
		onPullDownRefresh: function() {
			this.page = 1;
			this.doSearch(false);
		},
	}
</script>

<style>
	view {
		display: block;
	}

	.search-box {
		width: 100%;
		/* background-color: rgb(242, 242, 242); */
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
		background-color: #1E1F31;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		width: 100%;
		background-color: #111224;
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		/* border-radius: 20upx 20upx 0 0; */
		/* background-color: #fff; */
	}

	.keyword-entry-tap {
		background-color: #6a6eba;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		/* border-radius: 20upx 20upx 0 0; */
		background-color: #111224;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		font-weight: 700;
		color: #FFFFFF;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #FFFFFF;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(125, 125, 125);
		color: #FFFFFF;
	}

	.topBarBox {
		flex-flow: row;
		justify-content: flex-start;
		display: flex;
		text-align: center;
		border-bottom: #595959 1px solid;
		margin-bottom: 10rpx;

	}

	.topBar {
		width: 375rpx;
		color: #595959;
		font-style: oblique;
		font-size: 30rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
	}

	.topBar_active {
		border-bottom: #fff 1px solid;
		color: white;
		width: 375rpx;
		font-style: oblique;
		font-size: 35rpx;
		padding-bottom: 10rpx;
	}
</style>
